package com.village.zpark.controller;

import com.github.pagehelper.PageInfo;
import com.village.zpark.common.ResultMapUtil;
import com.village.zpark.pojo.Clinic;
import com.village.zpark.service.ClinicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/clinic")
public class ClinicController {
    @Autowired
    private ClinicService clinicService;


    @RequestMapping
    public String Clinic(){
        return "/clinic";
    }

    @RequestMapping(value = "/clinicPage")
    public String ClinicPage(){
        return "/clinicPage";
    }

    @RequestMapping(value = "/clinicQueryById")
    public Object ClinicQueryById(@RequestParam(name = "id",required = true) Integer id, Model model){
        Clinic clinic = clinicService.findById(id);
        model.addAttribute("obj",clinic);
        return "/clinicPage";
    }

    @RequestMapping(value = "/clinicList")
    @ResponseBody
    public Object ClinicList(){
        List<Clinic> clinicList = clinicService.findAll();
        return ResultMapUtil.getHashMapList(clinicList);
    }


    @RequestMapping(value = "/addclinic")
    @ResponseBody
    public Object addClinic(Clinic clinic){
        try {
            int i = clinicService.addClinic(clinic);
            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }


    @RequestMapping(value = "/updateclinic")
    @ResponseBody
    public Object updateClinic(Clinic clinic){
        try {
            int i =clinicService.updateClinic(clinic);
            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }

    @RequestMapping(value = "/deleteclinic")
    @ResponseBody
    public Object deleteClinic(Integer id){
        try {
            int i = clinicService.deleteClinicById(id);
            return ResultMapUtil.getHashMapDel(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }


    @RequestMapping(value = "/clinicQueryPage")
    @ResponseBody
    public Map<String, Object> clinicQueryPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit, String param) {
        PageInfo<Clinic> clinicPageInfo = clinicService.findPage(page, limit, param);
        Map<String,Object> map=new HashMap<>();
        map.put("code", 0);
        map.put("msg", "查询数据成功");
        map.put("count", clinicPageInfo.getTotal());
        map.put("data", clinicPageInfo.getList());
        return map;
    }
}
